#import "styles.typ": *
#import "@preview/numbly:0.1.0": numbly
#import "@preview/hydra:0.6.1": hydra

#set heading(numbering: numbly(
  "第{1}章",
  "{1}.{2}",
  "({3:a})",
))
#show heading: it => {
  if it.level == 1 [
    #pagebreak()
    #set page(header: none)  // 一级标题页禁用页眉
    #v(2em)
    #align(center, [
      #set text(fill: rgb("#8b4513"), weight: "bold", size: 2em)
      #smallcaps(it)
      #v(0.5em)
      #line(length: 50%, stroke: (paint: rgb("#bda22c"), thickness: 1pt))
    ])
    #v(1.5em)
  ] else if it.level == 2 [
    #pagebreak()
    #v(1em)
    #align(center, [
      #set text(fill: rgb("#556b2f"), weight: "bold", size: 1.3em)
      #smallcaps(it)
      #v(0.3em)
      #line(length: 30%, stroke: (paint: rgb("#bda22c"), thickness: 0.8pt))
    ])
    #v(0.8em)
  ] else [
    #set text(weight: "bold", size: 1.5em)
  ]
}





// 封面
#set page(columns: 1, header: none, paper: "a5", fill: rgb("#f8f4e8"))
#align(center)[
  #v(8em)
  #text(
    size: 2.8em, 
    weight: "bold", 
    fill: rgb("#5c3a21"),
    font: "SimSun"
  )[古文摘选]
  #v(2em)
  #text(
    size: 1.2em,
    fill: rgb("#7a5c44"),
    font: "KaiTi"
  )[辑录：某某某]
  #v(1em)
  #text(
    size: 1.0em,
    fill: rgb("#7a5c44"),
    font: "KaiTi"
  )[日期：癸卯年冬]
  #v(4em)
  #image("seal.gif", width: 3cm) // 添加印章图片
]
#pagebreak()

// 目录
#{
  let page-margin = 2.5cm // A5
  let magic-clearance = 2em
  set page(margin: (bottom: page-margin + magic-clearance, rest: page-margin), fill: none, header: none)
  show outline.entry: it => {
    show footnote: none
    it
  }

  set footnote.entry(separator: none)
  show footnote.entry: it => context v(-measure(it.note).height)
  outline(depth: 2)
}

#pagebreak()



// // 目录
// #set page(columns: 1)
// #align(center)[
//   #text(size: 2em, weight: "bold")[目录]
// ]
// #outline(depth: 2)  // 显示两级目录
// #pagebreak()

// 正文

#set page(
  paper: "a5",
  margin: (inside: 2.0cm, outside: 1.5cm, y: 1.5cm),
  header: {
    // 奇数页页眉
    context if calc.odd(counter(page).get().first()) {
      smallcaps(text(10pt, hydra(1), fill: rgb(206, 125, 163)))
      h(1fr)
      text(10pt, counter(page).display(), fill: rgb(122, 92, 68))
    }
    // 偶数页页眉
    else {
      text(10pt, counter(page).display(), fill: rgb(122, 92, 68))
      h(1fr)
      smallcaps(text(10pt, hydra(2), fill: rgb(206, 125, 163)))
    }
    line(length: 100%, stroke: (paint: rgb(189, 162, 44), thickness: 0.5pt, cap: "round"))
  },
  footer: counter(footnote).update(0),
  fill: none
)

#include "styles.typ"

#counter(page).update(0)

// 导入章节
#include "zuozhuan/zuozhuan.typ"
#include "zhanguoce/zhanguoce.typ"
#include "shiji/shiji.typ"


